#include <reg52.h>
typedef unsigned char uint8;
typedef unsigned int uint16;
typedef unsigned long uint32;
code uint8 number[] = {0xc0,0xf9,0xa4,0xb0,
				   	   0x99,0x92,0x82,0xf8,
				 	   0x80,0x90,0x88,0x83,
				 	   0xa7,0xa1,0x86,0x8e};

sbit ENLED = P1^4;
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
					   
uint8 a[6];
uint16 counter;
uint32 num;

//定时器初始化
void timer0_init()
{
	TMOD|=0x01;
	TMOD&=0xfd;
	TR0=1;
	TH0=0xfc;
	TL0=0x67;
}
//中断初始化
void int_init()
{
	EA=1;
	ET0=1;
}
//刷新led
void refresh_Led()
{
	static uint8 j = 0;
	switch(j)
    {
		case 0: 
			j++;
			ADDR0 = 0;ADDR1 = 0;ADDR2 = 0;
			P0 = number[a[0]];
			break;
		case 1:
			if(num/10>0){
				ADDR0 = 1;ADDR1 = 0;ADDR2 = 0;
				P0 = number[a[1]];
			}
			j++;
			break;
		case 2: 
			if(num/100>0){
				ADDR0 = 0;ADDR1 = 1;ADDR2 = 0;
				P0 = number[a[2]];
			}
			j++;
			break;
		case 3:
			if(num/1000>0){
				ADDR0 = 1;ADDR1 = 1;ADDR2 = 0;
				P0 = number[a[3]];
			}
			j++;
			break;
		case 4: 
			if(num/10000>0){
				ADDR0 = 0;ADDR1 = 0;ADDR2 = 1;
				P0 = number[a[4]];
			}
			j++;
			break;
		case 5: 
			if(num/100000>0){
				ADDR0 = 1;ADDR1 = 0;ADDR2 = 1;
				P0 = number[a[5]];
			}
			j = 0;
			break;
		default: break;
	}
}
//将num值分别按位存入a数组中
void setNumIntoArray()
{
	a[0]=num%10;
	a[1]=num/10%10;
	a[2]=num/100%10;
	a[3]=num/1000%10;
	a[4]=num/10000%10;
	a[5]=num/100000%10;
}

void main()
{
	ENLED=0;ADDR3=1;
	timer0_init();
	int_init();
	while(1);
}

void interruptLed() interrupt 3
{
	counter++;
	TH0=0xfc;
	TL0=0X67;
	if(1000==counter)
	{
		counter=0;
		num++;
		setNumIntoArray();
	}
	refresh_Led();
}